package hfuu.com.cn.teasystemapp;

import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.tree;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class treeaddacivity extends AppCompatActivity {

    List<land> landList;

    private tree tree;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_treeaddacivity);
        Intent old=getIntent();
        landList = new ArrayList<land>();
        landList = (List<land>) old.getSerializableExtra("landlist");
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tree=new tree();

        Spinner sptype= (Spinner) findViewById(R.id.treestate);
        Spinner spland=(Spinner)findViewById(R.id.treeland);
        final EditText treevarity= (EditText) findViewById(R.id.treevarity);
        sptype.setDropDownVerticalOffset(30);
        spland.setDropDownVerticalOffset(30);

        List data=new ArrayList();

        for (land land:
                landList) {
            data.add(land.getLandName());
        }
        ArrayAdapter ad=new ArrayAdapter(this,R.layout.landitem,R.id.land,data);
        spland.setAdapter(ad);
        spland.setVisibility(View.VISIBLE);

        Button okb= (Button) findViewById(R.id.ok);
        Button cab= (Button) findViewById(R.id.cancel);

        okb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String varity=treevarity.getText().toString().trim();

                if(varity.length()==0){
                    Toast.makeText(treeaddacivity.this,"请输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                tree.setTreeVarity(varity);
                Log.i("treetree",tree.toString());
                Gson gson=new Gson();
                String json= gson.toJson(tree);
                HttpUtils.doPost("tree/addprocad", json, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Toast.makeText(treeaddacivity.this, "网络连接失败", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res=response.body().string();
                        if(res.equals("success")){
                            Looper.prepare();
                            Toast.makeText(treeaddacivity.this, "插入成功", Toast.LENGTH_LONG).show();
                            Looper.loop();
                            return;
                        }


                    }
                });
                finish();
            }
        });


        cab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                treeaddacivity.this.finish();
            }
        });

        /**
         * 监听下拉列表
         */
        sptype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                if(m.equals("正常")){
                    tree.setTreeState(1);
                }else if(m.equals("异常")){
                    tree.setTreeState(2);

                }else if(m.equals("坏死")){
                    tree.setTreeState(0);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spland.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                for (land land:
                        landList) {
                    if(m.equals(land.getLandName()))
                        tree.setTreeLand(land.getLandId());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
